package com.example.studentmanager.controller;


import com.example.studentmanager.model.Student;
import com.example.studentmanager.model.vo.GradeVo;
import com.example.studentmanager.service.StudentService;
import com.example.studentmanager.service.impl.StudentServiceImpl;
import com.example.studentmanager.utils.DateUtil;
import com.sun.org.apache.xerces.internal.impl.xs.util.StringListImpl;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    Student student;


    @Autowired
    StudentService studentService;




    @RequestMapping("addStudent")
    public String add(@Valid String name, String phone, String password, char sex, String department, String classname, String major, String stunum){
        String result;
        //Student student=new Student(name,phone,password,sex,department,classname,major,stunum);
       student.setStunum(317);
        result=new StudentServiceImpl().addStu(student);
        return result;
    }

    @GetMapping("searchStudent")
    public ModelAndView searchStudent(String name){
       int stunum=31707042;
        Student student=new StudentServiceImpl().searchStudentByStunum(stunum);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("student",student);
        modelAndView.setViewName("searchStu");
        //model.addAttribute("ss",student);
        return modelAndView;
    }

    @RequestMapping(value = "/searchByName/{id}",method = RequestMethod.GET)
    public String searStudentByName(Model model,@PathVariable String id){
        String stuName="li";
        student.setStuName("li");
        List<Student> list=new ArrayList<Student>();
       // list=new StudentServiceImpl().searchAll();
       // list=studentService.searchAll();
        list=studentService.blurSelectStudent(student);
        return list.toString();
    }


    @RequestMapping("delStudentById")
    public String  delStudentById(Model model){
        Integer stunum=2324;
        new StudentServiceImpl().delStuById(stunum);
        return "success";
    }

    @RequestMapping("changeStu")
    public String changeStu(String stunum, String phone,String password, HttpSession session,String birth){
        student.setBirth(DateUtil.convertDateStr(birth));
        //student.setBirth(birth);
        student.setPhone(phone);
        student.setPassword(password);
        System.out.println(student.getBirth());
        studentService.updateStu(student);
        student=studentService.searchStudentByStunum(Integer.valueOf(stunum));
        session.setAttribute("student", student);
        return "back/student/infor";
    }

    @RequestMapping("findAll")
    public String searchAll(Integer page,String searchCol,String searchValue,Model model){

        //1.获取用户查询的当前页
        page=page==null?1:page;
        //2.获取每页显示的条数
        Integer rows=2;

        //获取用户输入的条件
        System.out.println(searchCol+"----"+searchValue);
        //调用业务层
        //3.求每页显示的成绩列表
        List<Student> students = studentService.searchAll(page,rows,searchCol,searchValue);
        //4.总记录数
        Integer counts =studentService.counts(searchCol, searchValue);
        //5.总页数
        Integer pages=counts%rows==0?counts/rows:counts/rows+1;
        //2.设置在域中

        model.addAttribute("page", page);
        model.addAttribute("pages", pages);
        model.addAttribute("counts", counts);
        model.addAttribute("searchCol",searchCol);
        model.addAttribute("searchValue",searchValue);
        //3.转发到前端页面
        model.addAttribute("students",studentService.searchAll(page,rows,searchCol,searchValue));
        return "back/student/index";

    }


}
